package swing.project.standard;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import work.utils.WorksUtils;

import com.h819.core.commons.Configurations;
import com.h819.core.file.MyFileUtils;
import com.h819.core.net.MyNetMonitorUtils;
import com.h819.core.utils.MyDateUtils;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @Title: Jpanel2.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-10-10
 * @version V1.0
 */
public class Jpanel2 extends JPanel {

	/**
	 * @wbp.nonvisual location=32,382
	 */
	private final JFileChooser fileChooser = new JFileChooser();
	private JLabel statuslable;
	private JButton buttonChoseFile;
	private static Logger log = Logger.getLogger(Jpanel2.class);
	private JLabel infolabel = new JLabel("");

	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("swing.project.standard.messages"); //$NON-NLS-1$
	// 加载配置文件
	PropertiesConfiguration conf = Configurations.getInstance(new File(BUNDLE
			.getString("app.config.filepath")));

	/**
	 * Create the panel.
	 */
	public Jpanel2() {

		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.GLUE_COLSPEC, },
				new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.GLUE_ROWSPEC, }));

		// 设置配置文件中指定的目录
		fileChooser.setCurrentDirectory(new File(conf
				.getString("config.pdf.download.root")));

		fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt",
				"txt"));

		JLabel maininfo = new JLabel(BUNDLE.getString("Jpanel3.maininfo.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(maininfo, "2, 4, 27, 1");

		buttonChoseFile = new JButton("\u9009\u62E9\u6587\u4EF6");
		buttonChoseFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// 首先判断数据库是否可以访问，再进行下一步。只要有一个不可用，则弹出提示框
				if (!MyNetMonitorUtils.isNetServiceOpen("129.9.200.235", 1521,
						2)
						|| !MyNetMonitorUtils.isFtpInServiceOpen(
								"129.9.200.236", 21, "read", "readpdf")) {

					// 此处弹出对话框
					JOptionPane.showMessageDialog(Jpanel2.this,
							" oracle 数据库或 ftp 不可用");
					return;

				}

				Component parent = (Component) arg0.getSource();
				setEnabled(false);

				int returnValue = fileChooser.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {

					File selectedFile = fileChooser.getSelectedFile();
					log.info(selectedFile.getPath());

					String extention = FilenameUtils.getExtension(
							selectedFile.getAbsolutePath()).toLowerCase();

					if (!extention.equals("txt")) {

						// 此处弹出对话框
						JOptionPane.showMessageDialog(Jpanel2.this,
								"请选择  txt 文件");
						return;
					}

					add(infolabel, "2, 14, 27, 1");
					infolabel.setText("select : " + selectedFile.getPath());

					// 设置下载目录，在配置文件中有规定
					String downLoadDir = conf
							.getString("config.pdf.download.root")
							+ "\\ftpFiles\\"
							+ MyDateUtils.getToday(MyDateUtils
									.getToday("yyyy-MM-dd_HH-mm-ss")) + "\\";

					log.info("downLoadDir :" + downLoadDir);
					File downLoadDirf = new File(downLoadDir);

					try {
						MyFileUtils.forceMkdir(downLoadDirf);
						// 打开文件夹
						java.awt.Desktop.getDesktop().open(
								new File(downLoadDir));

						// Runtime.getRuntime().exec(
						// "rundll32 SHELL32.DLL,ShellExec_RunDLL "
						// + "Explorer.exe ," + downLoadDir);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					WorksUtils wu = new WorksUtils();

					wu.downLoadPDFFilesFromServer(
							new File(selectedFile.getPath()), downLoadDirf);

					// Jpanel1.this.button.setEnabled(false);
				}
				// button.setEnabled(true);
			}
		});

		add(buttonChoseFile, "2, 12, right, default");

	}
}